package com.huixin.zhang.common.utils;

import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Array;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class DateTimeUtils {
	public static String YYYY = "yyyy";
	public static String YYYY_MM = "yyyy-MM";
	public  static String DATE_FORMATE_YM="yyyyMM";
	public static String YYYY_MM_DD = "yyyy-MM-dd";
	public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static String[] parsePatterns = {
			"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
			"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
	private static final SimpleDateFormat FORMAT = new SimpleDateFormat(DATE_FORMAT);

	/**
	 * 获取当前标准格式化日期时间
	 *
	 * @param
	 * @return
	 */
	public static String getDateTime() {
		return getDateTime(new Date());
	}

	/**
	 * 标准格式化日期时间
	 *
	 * @param date
	 * @return
	 */
	public static String getDateTime(Date date) {
		return FORMAT.format(date);
	}

	public static Date addDays(Date date, int amount) {
		Calendar calendar = GregorianCalendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		} else {
			calendar.setTimeInMillis(System.currentTimeMillis());
		}
		calendar.add(Calendar.DAY_OF_YEAR, amount);
		Date resultDate = calendar.getTime();
		return resultDate;
	}

	public static Date addMinutes(Date date, int amount) {
		Calendar calendar = GregorianCalendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		} else {
			calendar.setTimeInMillis(System.currentTimeMillis());
		}
		calendar.add(Calendar.MINUTE, amount);
		Date resultDate = calendar.getTime();
		return resultDate;
	}

	public static String dateFormate(Date date, String pattern) {
		if (date == null || StringUtils.isBlank(pattern))
			return null;
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		return formatter.format(date);
	}

	public static Date getAnyDayByNo(Date date, int anyDay) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(7, anyDay);
		return c.getTime();
	}

	public static Date getAnyMonthByNo(Date date, int anyMonth) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(2, anyMonth);
		return c.getTime();
	}

	public static Date getAnyHourByNo(Date date, int anyHour) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(11, anyHour);
		return c.getTime();
	}

	public static Date getAnyMinuteByNo(Date date, int anyMinute)
	{
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(12, anyMinute);
		return c.getTime();
	}

	public static Date textToDate(String date) {
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return bartDateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}

	public static Date textToDate(String date, String pattern) {
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(pattern);
		try {
			return bartDateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}

	public static String getFirstDayForMonth(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		gcLast.setTime(date);
		gcLast.set(5, 1);
		String day_first = df.format(gcLast.getTime());
		StringBuffer str = new StringBuffer().append(day_first);
		return str.toString();
	}

	public static String getLastDayForMonth(Date date) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(date);

		calendar.set(5, calendar.getActualMaximum(5));

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(calendar.getTime());
	}

	public static String getFirstDayForWeek(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		gcLast.setTime(date);
		gcLast.set(7, 1);
		String day_first = df.format(gcLast.getTime());
		StringBuffer str = new StringBuffer().append(day_first);
		return str.toString();
	}

	public static String getLastDayForWeek(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		gcLast.setTime(date);
		gcLast.set(7, 0);
		String day_first = df.format(gcLast.getTime());
		StringBuffer str = new StringBuffer().append(day_first);
		return str.toString();
	}

	public static String formatAll(Date date) {
		if (date == null)
			return null;
		return format(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static String format(Date date, String partten) {
		if (date == null || StringUtils.isBlank(partten))
			return null;
		return dateFormate(date, partten);
	}

	public static String getTime(Long second) {
		if (second.longValue() < 10L) {
			return "00:0" + second;
		}
		if (second.longValue() < 60L) {
			return "00:" + second;
		}
		if (second.longValue() < 3600L) {
			Long minute = Long.valueOf(second.longValue() / 60L);
			second = Long.valueOf(second.longValue() - minute.longValue() * 60L);
			if (minute.longValue() < 10L) {
				if (second.longValue() < 10L) {
					return "0" + minute + ":0" + second;
				}
				return "0" + minute + ":" + second;
			}
			if (second.longValue() < 10L) {
				return minute + ":0" + second;
			}
			return minute + ":" + second;
		}
		Long hour = Long.valueOf(second.longValue() / 3600L);
		Long minute = Long.valueOf((second.longValue() - hour.longValue() * 3600L) / 60L);
		second = Long.valueOf(second.longValue() - hour.longValue() * 3600L - minute.longValue() * 60L);
		if (hour.longValue() < 10L) {
			if (minute.longValue() < 10L) {
				if (second.longValue() < 10L) {
					return "0" + hour + ":0" + minute + ":0" + second;
				}
				return "0" + hour + ":0" + minute + ":" + second;
			}
			if (second.longValue() < 10L) {
				return "0" + hour + ":" + minute + ":0" + second;
			}
			return "0" + hour + ":" + minute + ":" + second;
		}
		if (minute.longValue() < 10L) {
			if (second.longValue() < 10L) {
				return hour + ":0" + minute + ":0" + second;
			}
			return hour + ":0" + minute + ":" + second;
		}
		if (second.longValue() < 10L) {
			return hour + ":" + minute + ":0" + second;
		}
		return hour + ":" + minute + ":" + second;
	}

	public static Long getDiffSecondsForDate(Date beginDate, Date endDate) {
		return Long.valueOf((endDate.getTime() - beginDate.getTime()) / 1000L);
	}

	/**
	 * 请求原始数据转换成Map
	 * @param data
	 * @param charset
	 * @return
	 */
	public static Map<String, String> paramCharsetConvert(Map<?, ?> data, String charset){

		Map<String, String> formattedParameters = new HashMap<String, String>(data.size());
		for (Map.Entry<?, ?> entry : data.entrySet()) {
			if (entry.getValue() == null || Array.getLength(entry.getValue()) == 0) {
				formattedParameters.put((String) entry.getKey(), null);
			} else {
				String str = (String) Array.get(entry.getValue(), 0);
				if(StringUtils.isNotBlank(str)) {
					formattedParameters.put((String) entry.getKey(),str);
				}
			}
		}

		return formattedParameters;
	}
}
